package com.order.service.impl;

import com.order.bean.Order;
import com.order.bean.Payment;
import com.order.mapper.OrderMapper;
import com.order.service.OrderService;
import com.order.util.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author weiruibai.vendor
 * Date: 2022/12/13 09:41
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    private static final String PAYMENT_SERVER = "http://PAYMENT-SERVER/payment";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderMapper mapper;

    public String order(Order order) {
        if (null != order) {
            order.setCreateAt(new Date());
        }
        log.info("start process payment");
        Payment payment = new Payment();
        payment.setUserId(order.getUserId());
        payment.setAmount(new BigDecimal(188));
        payment.setName("iPhone");
        CommonResult body = restTemplate.postForEntity(PAYMENT_SERVER + "/insert", payment, CommonResult.class).getBody();
        log.info("data: {}", body);
        log.info("end process payment");
        return mapper.insert(order) + "";
    }

    @Override
    public Order get(int id) {
        return mapper.get(id);
    }

    @Override
    public List<Order> getList() {
        return mapper.getList();
    }
}
